package cn.edu.stu.syllabus.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.edu.stu.syllabus.application.SyllabusApplication;
import rx.Subscriber;

/**
 * Created by daidaijie on 2017/3/23.
 * 封装的保存Bitmap为文件的类
 */

public class SaveBitmap2FileUtils {

    // 保存回调
    public interface OnSaveFileCallBack {
        void onSuccess(File file);

        void onFail(String msg);
    }

    public static void saveBitmap2File(Activity activity, final Bitmap bm, final String fileName, final int quality, final OnSaveFileCallBack onSaveFileCallBack) {
        // 通过RxPermissions对6.0及其以上的进行权限控制
        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Subscriber<Boolean>() {
                    @Override
                    public void onCompleted() {
                        bm.recycle();
                    }

                    @Override
                    public void onError(Throwable e) {
                        onSaveFileCallBack.onFail("授权失败");

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                            String folderName = Environment.getExternalStorageDirectory() + "/" + "Syllabus";
                            File folder = new File(folderName);
                            if (!folder.exists()) {
                                folder.mkdirs();
                            }
                            File myCaptureFile = new File(folder, fileName);
                            if (!myCaptureFile.exists()) {
                                try {
                                    myCaptureFile.createNewFile();
                                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
                                    bm.compress(Bitmap.CompressFormat.JPEG, quality, bos);
                                    bos.flush();
                                    bos.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    onSaveFileCallBack.onFail("保存失败");
                                    return;
                                }
                            }
                            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                            Uri uri = Uri.fromFile(myCaptureFile);
                            intent.setData(uri);
                            SyllabusApplication.getContext().sendBroadcast(intent);
                            onSaveFileCallBack.onSuccess(myCaptureFile);
                        } else {
                            onSaveFileCallBack.onFail("截图失败");
                        }
                    }
                });

    }
}
